RRFReranker module
RRFReranker
Bases: Module
Merge multiple search results with Reciprocal Rank Fusion (RRF).
Takes a list of result data models — each a GenericResult whose
result field is a ranked list of rows — and fuses their rankings
into a single ranked list. A row's fused score is
``score(row) = sum over lists of 1 / (k_rank + rank)``
where rank is the row's 1-based position in each list. RRF needs
only the ordering of each list, so it merges heterogeneous result
sets (similarity, full-text, regex, graph) without having to
normalize their incompatible score scales.
Rows are matched across lists by id_key when given, otherwise by
a canonical signature of the whole row. The fused rrf_score is
written onto each returned row; the output is a GenericResult
sorted by descending score and truncated to k. None inputs
are ignored, so it composes with optional retrieval branches.
Example:
import synalinks
import asyncio
class Query(synalinks.DataModel):
query: str = synalinks.Field(description="The user question")
async def main():
kb = synalinks.KnowledgeBase(uri="duckdb://docs.db", data_models=[Document])
lm = synalinks.LanguageModel(model="ollama/mistral")
inputs = synalinks.Input(data_model=Query)
vector_hits = await synalinks.SimilaritySearch(
knowledge_base=kb, language_model=lm, data_model=Document,
)(inputs)
keyword_hits = await synalinks.FullTextSearch(
knowledge_base=kb, language_model=lm, data_model=Document,
)(inputs)
fused = await synalinks.RRFReranker(k=10, id_key="id")(
[vector_hits, keyword_hits]
)
program = synalinks.Program(inputs=inputs, outputs=fused)
asyncio.run(main())
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k_rank
|
int
|
RRF smoothing constant. Lower values weight top-ranked rows more strongly. Defaults to 60. |
60
|
k
|
int
|
Maximum number of fused rows to return. |
None
|
id_key
|
str
|
Row field used to identify the same row across
lists. When |
None
|
name
|
str
|
Optional. The name of the module. |
None
|
description
|
str
|
Optional. The description of the module. |
None
|
trainable
|
bool
|
Whether the module's variables should be trainable. |
False
|
Source code in synalinks/src/modules/rerankers/rrf_reranker.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | |